var service_user = require("../../service/user.js");
var gwang = require("../../service/gwang.js");
var service_common = require("../../service/common.js");
Page({
  data: {
    userInfo: {},
    headimgurl: "",
    photo_css: "display:none;",
    slide_tip: "display:none;",
    tc_userInfo:false,
    pageContent:{},
    openid:""
  },
  /** 
   * 跳转投诉页面
   */
  toReport: function () {
    var that = this
    wx.navigateTo({
      url: '../report/report?openid=' + that.data.openid,
    })
  },
  getHongBao: function () {
    var that = this;
    wx.navigateToMiniProgram({
      appId: 'wx36e289fcc22707ec',
      path: 'pages/send/send?openid=' + getApp().globalData.userInfo.openid + "&id=" + that.data.userInfo.hongBao + "&from=yzmp",
    })
  },
  putHongBao: function () {
    wx.navigateToMiniProgram({
      appId: 'wx36e289fcc22707ec',
      path: "pages/index/index?openid=" + getApp().globalData.userInfo.openid + "&from=yzmp",
    })
  },
  /**
   * 保存用户到通讯录
   */
  saveContact: function () {
    var that = this;
    if (this.data.userInfo.mobile_visible) {
      wx.showModal({
        title: '提示',
        content: '对方已设置隐藏手机号码',
        showCancel: false,
        success: res => {
          return;
        }
      })
    } else {
      wx.addPhoneContact({
        firstName: that.data.userInfo.nickname,
        mobilePhoneNumber: that.data.userInfo.mobile,
        weChatNumber: that.data.userInfo.email,
        organization: that.data.userInfo.company,
        title: that.data.userInfo.job,
        email: that.data.userInfo.wechat,
      })
    }
  },
  // 拨打电话
  callPhone:function(e){
    console.log(e)
    wx.makePhoneCall({
      phoneNumber: '',
    })
  },
  // 前往地图
  goMap:function(){
      wx.switchTab({
        url: '/pages/aMap/aMap',
      })
  },
  // 跟某某聊天
  gochat: function () {
    var p1 = {
      uid_head: getApp().globalData.userInfo.src,
      name: getApp().globalData.userInfo.nickname,
      uid: getApp().globalData.userInfo.openid
    }
    var p2 = {
      uid_head: this.data.userInfo.src,
      name: this.data.userInfo.nickname,
      uid: this.data.userInfo.openid
    }
    wx.setStorageSync('p1', p1)
    wx.setStorageSync('p2', p2)
    console.log(this.data.userInfo.nickname)
    wx.navigateTo({
      url: '/pages/webChat/webChat?title_name=' + this.data.userInfo.nickname,
    })
  },
  onLoad: function (options) {
    var that = this;
    console.log(options)
    this.setData({
      options: options
    })
    if (options.data != undefined && options.data !='0'){
      var optionss = JSON.parse(options.data) 
      getApp().globalData.uniq_openid = optionss.uniq_openid
      wx.setStorageSync('token__', optionss.token)
      wx.setStorageSync('token_key', optionss.token_key)
      service_common.fetchData(
        service_common.getUrl("user.getOtherUserMpInfo"), {
          openid: getApp().globalData.userInfo.openid,
          openid2: optionss.openid
        },
        (res) => {
          if (res.data.code == 0) {
            wx.setStorageSync('userInfo', res.data.data);
            getApp().globalData.userInfo = res.data.data
            getApp().globalData.openid = res.data.data.openid
            getApp().globalData.unionid = res.data.data.unionid
            this.setData({
              openid: options.openid
            })
            wx.showLoading({
              title: '加载中',
              mask: true
            })
            this.getData()
          }
        },
        "GET"
      )
    }else{
      this.setData({
        openid: options.openid
      })
      wx.showLoading({
        title: '加载中',
        mask: true
      })

      this.getData()
    }
   
  },
  onUnload:function(){
    if (this.data.options.data != undefined && this.data.options.data != '0'){
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },
  getData:function(){
    var that=this
    service_common.fetchData(
      service_common.getUrl("user.getOtherUserMpInfo"),
      {
        openid: getApp().globalData.userInfo.openid,
        openid2: that.data.openid
      },
      function (res) {
        if (res.data.data.wechat == "null" || res.data.data.wechat == undefined) {
          res.data.data.wechat = "";
        }
        that.setData({
          userInfo: res.data.data
        })
        wx.setNavigationBarTitle({
          title: res.data.data.nickname + " 的名片"
        })
        service_user.getUserImage(res.data.data.openid, function (res2) {
          that.setData({
            image_array: res2.data.data,
          })
          if (res2.data.data.length > 0) {
            that.setData({
              photo_css: ""
            })
          }

          if (res2.data.data.length > 3) {
            that.setData({
              slide_tip: ""
            })
          } else {
            that.setData({
              slide_tip: "display:none;"
            })
          }
          wx.hideLoading()
        })
      },
      "GET"
    )
  },
  // 授权
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      this.setData({
        tc_userInfo: false
      })
      this.login()
    }
  },
  login: function () {
    var that = this;
    if (that.data.openid != "") {
      if (getApp().globalData.userInfo.openid == that.data.openid) {
        wx.switchTab({
          url: '/page/index/index',
        })
      }
      service_common.fetchData(
        service_common.getUrl("user.getOtherUserMpInfo"),
        {
          openid: getApp().globalData.userInfo.openid,
          openid2: that.data.openid
        },
        function (res) {
          if (res.data.data.wechat == "null" || res.data.data.wechat == undefined) {
            res.data.data.wechat = "";
          }
          that.setData({
            userInfo: res.data.data
          })
          wx.setNavigationBarTitle({
            title: res.data.data.nickname + " 的名片"
          })
          if (res.data.data.gw != 0) {
            that.getPageContent(res.data.data.gw.id);
          }

          service_user.getUserImage(res.data.data.openid, function (res2) {
            that.setData({
              image_array: res2.data.data,
            })
            if (res2.data.data.length > 0) {
              that.setData({
                photo_css: ""
              })
            }

            if (res2.data.data.length > 3) {
              that.setData({
                slide_tip: ""
              })
            } else {
              that.setData({
                slide_tip: "display:none;"
              })
            }
            wx.hideLoading()
          })
        },
        "GET"
      )
    }
  },
  // 获取页面信息
  getPageContent: function (id) {
    // 获取页面书
    gwang.getPageContent(getApp().globalData.userInfo.openid, id, res => {
      this.setData({
        pageContent: res.data.data
      })
    })
  },
  //人气formId
  addFormId:function(e){
    add_form_id(e.detail.formId);
    console.log(e.detail.formId);
  },
  onShow: function () {
    var that = this;

  },
  show_head_image: function (e) {
    console.log(this.data.userInfo.src)
    var that = this;
    add_form_id(e.detail.formId)
    
    wx.previewImage({
      urls: [that.data.userInfo.src]
    })
  },
  //复制邮箱
  copy_wechat: function () {
    var that = this
    wx.setClipboardData({
      data: that.data.userInfo.email,
    })
    wx.showToast({
      title: '复制成功',
      icon: 'success',
      duration: 300,
      mask: true,
    });
  },
  // 复制
  Copy:function(e){
    wx.setClipboardData({
      data: e.currentTarget.dataset.msg,
    })
    wx.showToast({
      title: '复制成功',
      icon: 'success',
      duration: 300,
      mask: true,
    });
  },
  // 复制微信
  copy_email: function () {
    var that = this
    wx.setClipboardData({
      data: that.data.userInfo.wechat,
    })
    wx.showToast({
      title: '复制成功',
      icon: 'success',
      duration: 300,
      mask: true,
    });
  },
  //图片预览
  showPhoto: function (e) {
    var id = e.currentTarget.id;
    id = parseInt(id);
    var array = [];
    for (var i = 0; i < this.data.userInfo.imgs.length; i++) {
      array[i] = this.data.userInfo.imgs[i].b;
    }
    wx.previewImage({
      current: array[id],
      urls: array
    })
  },
  //百万曝光
  gotofind: function (e) {
    add_form_id(e.detail.formId)
    wx.switchTab({
      url: "/pages/find2/find2"
    })
  },
  //代理升级
  gotokaidaili: function(e){
    add_form_id(e.detail.formId);
    wx.navigateTo({
      url: "/pages/kaidaili/kaidaili"
    })
  },
  //打开我的名片
  openMyCard: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  toEdit: function () {
    wx.redirectTo({
      url: '../edit/edit',
    })
  },
  //打电话
  callPhone: function () {
    var that = this;
    if (this.data.userInfo.mobile_visible==0) {
      if (that.data.userInfo.mobile != "") {
        wx.makePhoneCall({
          phoneNumber: that.data.userInfo.mobile
        })
      }
    }
  },
  //喜欢
  loveCard: function () {
    var that = this;
    service_user.loveAdd(this.data.openid, function (res) {
      wx.showToast({
        title: '点赞成功',
        mask: true,
        complete: res => {
          that.getData();
        }
      })
    });
  },
  // 去其他人的官网
  goGwang:function(){
    wx.navigateTo({
      url: '/pages/handle/details/details?id=' + this.data.userInfo.gw.id,
    })
  },
  //取消喜欢
  cancelLoveCard: function () {
    var that = this;
    service_user.loveDrop(this.data.openid, function (res) {
      wx.showToast({
        title: '取消点赞',
        mask: true,
        complete: res => {
          that.getData();
        }
      })
    });
  },
  isToday: function (str) {
    if (new Date(str).toDateString() === new Date().toDateString()) {
      // 当天
      return 1
    } else if (new Date(str) < new Date()) {
      return 2
    }
  },
  //收藏名片
  collectCard: function (e) {
    var that = this;
    add_form_id(e.detail.formId)
    if (getApp().globalData.num==0){
      if (this.data.openid == getApp().globalData.userInfo.openid) {
        wx.showToast({
          title: '自己不需要收藏',
          image: "../../image/cancel.png",
          mask: true,
        })
      } else {
        service_user.collectAdd(this.data.openid, function (res) {
          if (res.data.code == 0) {
            wx.showToast({
              title: '收藏成功',
              mask: true,
              complete: res => {
                getApp().globalData.num += 1
                wx.setStorage({
                  key: 'num',
                  data: getApp().globalData.num,
                })
                that.getData();
              }
            })
          } else if (res.data.code == 10003) {
            wx.showToast({
              title: '不需要重复收藏',
              image: "../../image/cancel.png",
              mask: true
            })
          } else {
            wx.showToast({
              title: '收藏失败，请重试',
              mask: true,
              complete: res => {
                that.getData();
              }
            })
          }
        });
      }
     
    }else{
      
      if (that.data.openid == getApp().globalData.userInfo.openid) {
          wx.showToast({
            title: '自己不需要收藏',
            image: "../../image/cancel.png",
            mask: true,
          })
        } else {
          service_user.collectAdd(that.data.openid, function (res) {
            if (res.data.code == 0) {
              wx.showToast({
                title: '收藏成功',
                mask: true,
                complete: res => {
                  getApp().globalData.num += 1
                  wx.setStorage({
                    key: 'num',
                    data: getApp().globalData.num,
                  })
                  that.getData();
                }
              })
            } else if (res.data.code == 10003) {
              wx.showToast({
                title: '不需要重复收藏',
                image: "../../image/cancel.png",
                mask: true
              })
            } else {
              wx.showToast({
                title: '收藏失败，请重试',
                mask: true,
                complete: res => {
                  that.getData();
                }
              })
            }
          });
        }

      
    }
    
  },
  //取消收藏名片
  cancelCollectCard: function (e) {
    var that = this;
    service_user.collectDrop(that.data.openid, function (res) {
      wx.showToast({
        title: '已取消收藏',
        mask: true,
        complete: res => {
          that.getData();
        }
      })
    });
  },
  no_action: function (e) {
    add_form_id(e.detail.formId);
  }
 
})
//添加form_id
function add_form_id(form_id) {
  service_user.addFormId(form_id);
}
